package com.dsw.demo.rabbitmq.controller;


import com.dsw.demo.rabbitmq.config.DelayRabbitMQConfig;
import com.dsw.demo.rabbitmq.config.DirectRabbitMQConfig;
import com.dsw.demo.rabbitmq.config.FanoutRabbitMQConfig;
import com.dsw.demo.rabbitmq.config.TopicRabbitMQConfig;
import com.dsw.demo.rabbitmq.entity.TaskInfo;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class SendController implements RabbitTemplate.ConfirmCallback {
    private RabbitTemplate rabbitTemplate;

    /**
     * 构造方法注入
     */
    @Autowired
    public SendController(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
        rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果为单例的话，那回调就是最后设置的内容
    }

    /**
     * 向direct exchange 消息队列中发送消息
     *
     * @param msg 消息内容
     * @author keyu.wang
     * @date 2018/5/7 16:51
     */
    @RequestMapping("directSend")
    public String directSend(String msg) {
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationId = new CorrelationData(uuid);
        rabbitTemplate.convertAndSend(DirectRabbitMQConfig.DIRECTEXCHANGE, DirectRabbitMQConfig.DIRECTROUTINGKEY, msg, correlationId);
        return null;

    }


    /**
     * 向fanout exchange 消息队列中发送消息
     *
     * @param msg 消息内容
     * @author keyu.wang
     * @date 2018/5/7 16:51
     */
    @RequestMapping("fanoutSend")
    public String fanoutSend(String msg) {
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationId = new CorrelationData(uuid);
        rabbitTemplate.convertAndSend(FanoutRabbitMQConfig.FANOUT_EXCHANGE, "", msg, correlationId);
        return null;

    }

    /**
     * 向topic exchange 消息队列中发送消息
     *
     * @param msg 消息内容
     * @author keyu.wang
     * @date 2018/5/7 16:51
     */
    @RequestMapping("topicSend")
    public String topicSend(String msg) {
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationId = new CorrelationData(uuid);
        rabbitTemplate.convertAndSend(TopicRabbitMQConfig.TOPICEXCHANGE, TopicRabbitMQConfig.TOPICROUTINGKEY, msg, correlationId);
        return null;

    }

    /**
     * 向延时队列推送消息
     *
     * @param msg
     * @return
     */
    @RequestMapping("delaySend")
    public String delaySend(String msg) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationId = new CorrelationData(uuid);
        msg = msg + " " + sdf.format(new Date());
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setId(msg);
        taskInfo.setType(0);
        taskInfo.setMemo(msg);
        taskInfo.setCreateTime(new Date());
        this.rabbitTemplate.convertAndSend(DelayRabbitMQConfig.DELAY_EXCHANGE, DelayRabbitMQConfig.DELAY_ROUTING_KEY, taskInfo,
                message -> {
                    message.getMessageProperties().setDelay(1000 * 60 * 1);
                    return message;
                }, correlationId);
        System.out.println("发送队列时间" + sdf.format(new Date()));
        return null;
    }


    /**
     * 消息的回调，主要是实现RabbitTemplate.ConfirmCallback接口
     * 注意，消息回调只能代表成功消息发送到RabbitMQ服务器，不能代表消息被成功处理和接受
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {
            System.out.println("回调id:" + correlationData.getId() + " 消息成功消费");
        } else {
            System.out.println("回调id:" + correlationData.getId() + " 消息消费失败:" + cause);
        }
    }


}
